import py_trees
import time


# 行为节点
class SayHello(py_trees.behaviour.Behaviour):
    def __init__(self, name, target):
        super().__init__(name)
        self.name = name
        self.target = target

    def update(self):
        print(f"{self.name} say: {self.target}")
        return py_trees.common.Status.SUCCESS


# 创建行为树
def create_behavior_tree():
    # 根节点
    root = py_trees.composites.Sequence("root根序列", memory=True)
    # 行为节点
    lili_say = SayHello("lili", "hello world")
    jack_say = SayHello("jack", "你好")
    # 构建行为树
    root.add_children(
        [
            lili_say,
            jack_say,
        ]
    )
    return root


def main():
    # 创建行为树
    behaviour_tree = create_behavior_tree()

    # 打印树结构
    print("行为树结构:")
    print(py_trees.display.ascii_tree(behaviour_tree))

    # 运行行为树
    count = 3
    while count > 0:
        behaviour_tree.tick_once()
        count -= 1
        time.sleep(1)


if __name__ == "__main__":
    main()

"""
行为树结构:
{-} root根序列
    --> lili
    --> jack

lili say: hello world
jack say: 你好
lili say: hello world
jack say: 你好
lili say: hello world
jack say: 你好
"""
